import time
import json
import os
from utils.util import selectForOne


async def run(browser):
    path = './scripts'  # 脚本文件夹
    files = os.listdir(path)  # 获取所有脚本
    while True:
        for fileName in files:
            print('####################(' + fileName + ')--开始执行####################')
            for item in json.loads(open(path + '/' + fileName, encoding='utf-8').read()):
                try:
                    if item['mode'] == 'table':
                        tu(browser, item['tableUpdateList'], item['row'])
                    elif item['mode'] == 'click' or item['mode'] == 'send_keys':
                        # print(len(browser.find_elements_by_xpath(str(item['target']))))
                        target = browser.find_element_by_xpath(str(item['target']))
                        fun = getattr(target, item['mode'])
                        value = item['value'] if item['mode'] == 'send_keys' else False
                        if value:
                            fun(value)
                        else:
                            fun()
                    print(item['describe'], '--执行成功')
                    time.sleep(0.5)
                except Exception as r:
                    print(item['describe'], '--发生错误', r)
                    time.sleep(0.3)


    time.sleep(5)
    browser.close()
    return


def tu(browser, tableUpdateList, row):
    # 固定的表头找index
    header = browser.find_elements_by_class_name('el-table__fixed-header-wrapper')
    for item in header:
        for index, item2 in enumerate(item.find_elements_by_tag_name('th')):
            for item3 in tableUpdateList:
                if item3['columnName'] == item2.text:
                    item3['index'] = index
                    item3['isFixed'] = True
    time.sleep(0.1)
    # 正常的表头找index
    header = browser.find_elements_by_class_name('el-table__header-wrapper')
    for item in header:
        for index, item2 in enumerate(item.find_elements_by_tag_name('th')):
            for item3 in tableUpdateList:
                if item3['columnName'] == item2.text:
                    item3['index'] = index
                    item3['isFixed'] = False

    index_ = 0
    while index_ < row:
        # 固定表格找td
        table = browser.find_element_by_class_name('el-table__fixed-body-wrapper')
        tableTr = table.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')[index_]
        for index, item in enumerate(tableTr.find_elements_by_tag_name('td')):
            for item2 in tableUpdateList:
                if index == item2['index'] and item2['isFixed']:
                    item2['td'] = item

        # 正常表格找td
        table = browser.find_element_by_class_name('el-table__body')
        tableTr = table.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')[index_]
        for index, item in enumerate(tableTr.find_elements_by_tag_name('td')):
            for item2 in tableUpdateList:
                if index == item2['index'] and not item2['isFixed']:
                    item2['td'] = item

        # 处理表格里的逻辑
        for item in tableUpdateList:
            if item['columnName'] == '存货':
                item['td'].click()
                time.sleep(0.1)
                item['td'].click()
                time.sleep(0.1)
                selectForOne(browser, item['value'])
                time.sleep(0.2)
                if browser.find_elements_by_xpath("//*[@aria-label = '产品关联查询']"):
                    browser.find_element_by_xpath("//*[@aria-label = '产品关联查询']").find_element_by_class_name(
                        'el-dialog__headerbtn').click()
                time.sleep(0.2)
            elif item['columnName'] == '件数':
                if item['td'].find_element_by_tag_name('input').text == '':
                    item['td'].find_element_by_tag_name('input').send_keys(item['value'])
                    time.sleep(0.1)

        index_ = index_ + 1
